package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.MessageReceive;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * 消息adapter
 * Created by Aaron on 2015/7/16.
 */
public class NewsItemAdapter extends BaseAdapter {

    private Context ctx;
    private List<MessageReceive.MessageData> msgs;
    private DisplayImageOptions options;

    public NewsItemAdapter(Context ctx, List<MessageReceive.MessageData> msgs) {
        this.ctx = ctx;
        this.msgs = msgs;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_head_default)
                .showImageForEmptyUri(R.drawable.icon_head_default)
                .showImageOnFail(R.drawable.icon_head_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    @Override
    public int getCount() {

//        LogUtil.e("TAG", "getCount==>" + size.size());

        return msgs.size();
    }

    public void setMsgs(List<MessageReceive.MessageData> msgs) {
        this.msgs = msgs;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder=null;
        if(convertView==null){
            convertView=View.inflate(ctx, R.layout.item_news_message,null);
            viewHolder=new ViewHolder();
            viewHolder.ci_pic=(CircleImageView)convertView.findViewById(R.id.ci_pic);
            viewHolder.tv_title= (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_content= (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.iv_red_point= (ImageView) convertView.findViewById(R.id.iv_red_point);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_title.setText(CommonUtils.isNull(msgs.get(position).title)?"": msgs.get(position).title);
        viewHolder.tv_time.setText(CommonUtils.isNull(msgs.get(position).sendTime)?"": msgs.get(position).sendTime);
        viewHolder.tv_content.setText(CommonUtils.isNull(msgs.get(position).content) ? "" : msgs.get(position).content);
       //判断收件人还是发件人所
        ImageLoader.getInstance().displayImage(msgs.get(position).sendPeopleUrl, viewHolder.ci_pic,options);

        if ("0".equals(msgs.get(position).flag)){
            viewHolder.iv_red_point.setVisibility(View.VISIBLE);
        }else {
            viewHolder.iv_red_point.setVisibility(View.INVISIBLE);
        }
        return convertView;

    }

    private class ViewHolder {
        private CircleImageView ci_pic;
        private TextView tv_title;
        private TextView tv_time;
        private TextView tv_content;
        private ImageView iv_red_point;
    }
}
